package com.tin.restfinder;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.nio.Buffer;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

public class Data{

	public static AppConfiguration conf=new AppConfiguration();
	public static User user=new User();
	public static Restaurant restdetails; 

	public static void LoadUser(Context context)
	{
		//user.LoadUser();
	}
	public static void SaveUser(Context context)
	{
		//TODO saveuser
	}
	public static void LoadConfiguration(Context context)
	{
		String FILENAME_rest ="restlist.rff";
		String FILENAME_comments="comments.rff";
		String FILENAME_tags="tags.rff";
		String FILENAME_lastupdate="lastupdate.rff";
		
		// wczytywanie listy restaruacji
		{
			FileInputStream fos;
			try {
				byte[] buffer= new byte [100000];
				fos = context.openFileInput(FILENAME_rest);
				fos.read(buffer);
				fos.close();
				String JSONString = new String(buffer);
				JSONArray jArray = new JSONArray(JSONString); 
				for(int i=0;i<jArray.length();i++)
				{
					JSONObject json_data = jArray.getJSONObject(i);
					Restaurant temp=new Restaurant();
					temp.setRest_id(json_data.getInt("rest_id"));
					temp.setName(json_data.getString("name"));
					temp.setStreet(json_data.getString("street"));
					temp.setCity(json_data.getString("city"));
					temp.setTelephone(json_data.getString("telephone"));
					temp.setLatitude(json_data.getDouble("latitude"));
					temp.setLongitude(json_data.getDouble("longitude"));
					temp.setDescription(json_data.getString("description"));
					temp.setRating(json_data.getDouble("rating"));
					temp.setRated(json_data.getInt("rated"));
					// trzeba dodac logo
					Data.conf.getList().add(temp);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			}catch (IllegalArgumentException e) {
				e.printStackTrace();
			}


			
		}
		// wczytywanie komentarzy
		{
			FileInputStream fos;
			try {
				byte[] buffer= new byte [100000];
				fos = context.openFileInput(FILENAME_comments);
				fos.read(buffer);
				fos.close();
				String JSONString = new String(buffer);
				JSONArray jArray = new JSONArray(JSONString); 
				for(int i=0;i<jArray.length();i++)
				{
					JSONObject json_data = jArray.getJSONObject(i);
					if(Data.conf.getList().size()>0)
						for(int l=0;l<Data.conf.getList().size();l++)
							if(json_data.getInt("rest_id")==Data.conf.getList().get(l).getRest_id())
							{
								Comments temp=new Comments (json_data.getString("username"),json_data.getString("content"),json_data.getString("date"));
								Data.conf.getList().get(l).getComments().add(temp);
								break;
							}
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			}

		}
		// wczytywanie tagow
		{
			FileInputStream fos;
			try {
				byte[] buffer= new byte [100000];
				fos = context.openFileInput(FILENAME_tags);
				fos.read(buffer);
				fos.close();
				String JSONString = new String(buffer);
				JSONArray jArray = new JSONArray(JSONString); 
				for(int i=0;i<jArray.length();i++)
				{
					JSONObject json_data = jArray.getJSONObject(i);
					if(Data.conf.getList().size()>0)
						for(int l=0;i<Data.conf.getList().size();l++)
							if(json_data.getInt("rest_id") ==Data.conf.getList().get(l).getRest_id())
							{
								Data.conf.getList().get(l).getTags().add(json_data.getString("tag"));
								return;
							}
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			}

		}
		// save lastupdate
		{
			FileInputStream fos;
			try {
			byte[] buffer= new byte [100];
			fos = context.openFileInput(FILENAME_lastupdate);
			fos.read(buffer);
			fos.close();		
			Date date=new Date();
			String temp=new String(buffer,"UTF-8");
			temp=temp.trim();
			date.setTime(Long.parseLong(temp));
			Data.conf.setLast_update(date);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} 
		}
	}
	public static void SaveConfiguration(Context context)
	{
		String FILENAME_rest ="restlist.rff";
		String FILENAME_comments="comments.rff";
		String FILENAME_tags="tags.rff";
		String FILENAME_lastupdate="lastupdate.rff";
		
		// Zapisywanie restauracji
		{

			try {			
				JSONArray myJSONArray = new JSONArray();
				for(int i=0;i<Data.conf.getList().size();i++)
				{
					JSONObject myJSON= new JSONObject();
					myJSON.put("rest_id", Data.conf.getList().get(i).getRest_id());
					myJSON.put("name", Data.conf.getList().get(i).getName());
					myJSON.put("street", Data.conf.getList().get(i).getStreet());
					myJSON.put("city", Data.conf.getList().get(i).getCity());
					myJSON.put("telephone", Data.conf.getList().get(i).getTelephone());
					myJSON.put("latitude", Data.conf.getList().get(i).getLatitude());
					myJSON.put("longitude", Data.conf.getList().get(i).getLongitude());
					myJSON.put("rating", Data.conf.getList().get(i).getRating());
					myJSON.put("description", Data.conf.getList().get(i).getDescription());
					myJSON.put("rated", Data.conf.getList().get(i).getRated());
					// trzeba dodac logo
					myJSONArray.put(myJSON);
				}
				FileOutputStream fos = context.openFileOutput(FILENAME_rest, Context.MODE_PRIVATE);
				fos.write(myJSONArray.toString().getBytes());
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		// zapiswyanie komentarzy
		{

			try {
				JSONArray myJSONArray = new JSONArray();
				for(int l=0;l<Data.conf.getList().size();l++)
					for(int i=0;i<Data.conf.getList().get(l).getComments().size();i++)
					{
						JSONObject myJSON= new JSONObject();
						myJSON.put("rest_id", Data.conf.getList().get(l).getRest_id());
						myJSON.put("username", Data.conf.getList().get(l).getComments().get(i).getUsername());
						myJSON.put("content", Data.conf.getList().get(l).getComments().get(i).getContent());
						myJSON.put("date", Data.conf.getList().get(l).getComments().get(i).getDate());
						myJSONArray.put(myJSON);
					}
				FileOutputStream fos = context.openFileOutput(FILENAME_comments, Context.MODE_PRIVATE);
				fos.write(myJSONArray.toString().getBytes());
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		// zapisywanie tagow
		{

			try {
				JSONArray myJSONArray = new JSONArray();
				for(int l=0;l<Data.conf.getList().size();l++)
					for(int i=0;i<Data.conf.getList().get(l).getTags().size();i++)
					{
						JSONObject myJSON= new JSONObject();
						myJSON.put("rest_id", Data.conf.getList().get(l).getRest_id());
						myJSON.put("tag", Data.conf.getList().get(l).getTags().get(i));
						myJSONArray.put(myJSON);
					}
				FileOutputStream fos = context.openFileOutput(FILENAME_tags, Context.MODE_PRIVATE);
				fos.write(myJSONArray.toString().getBytes());
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		// zapis last update
		{
			try {
				String temp = ""+Data.conf.getLast_update().getTime();
				FileOutputStream fos = context.openFileOutput(FILENAME_lastupdate, Context.MODE_PRIVATE);
				fos.write(temp.getBytes());
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			} 
		}
	}
	public static void SetRestDetailsByName(String name)
	{
		for(int i=0;i<Data.conf.getList().size();i++)
			if(Data.conf.getList().get(i).getName().equals(name))
			{
				restdetails=Data.conf.getList().get(i);
				return;
			}
	}

}